package com.ruoyi.category.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import com.ruoyi.category.domain.SysPart;
import com.ruoyi.category.mapper.SysPartMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.category.mapper.SysBackpackMapper;
import com.ruoyi.category.domain.SysBackpack;
import com.ruoyi.category.service.ISysBackpackService;

/**
 * 背包Service业务层处理
 * 
 * @author YJF
 */
@Service
public class SysBackpackServiceImpl implements ISysBackpackService 
{
    @Autowired
    private SysBackpackMapper sysBackpackMapper;

    @Autowired
    private SysPartMapper sysPartMapper;

    /**
     * 查询背包
     * 
     * @param backpackId 背包主键
     * @return 背包
     */
    @Override
    public SysBackpack selectSysBackpackByBackpackId(Long backpackId)
    {
        SysBackpack backpackInfo = sysBackpackMapper.selectSysBackpackByBackpackId(backpackId);
        List<Long> partGroupIds = Arrays.stream(backpackInfo.getPartGroup().split(",")).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
        List<Long> partIds = Arrays.stream(backpackInfo.getParts().split(",")).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
        backpackInfo.setPartGroupIds(partGroupIds);
        backpackInfo.setPartIds(partIds);
        return backpackInfo;
    }

    /**
     * 查询背包列表
     * 
     * @param sysBackpack 背包
     * @return 背包
     */
    @Override
    public List<SysBackpack> selectSysBackpackList(SysBackpack sysBackpack)
    {
        List<SysBackpack>  backpackList = sysBackpackMapper.selectSysBackpackList(sysBackpack);

        //获取配件ID集合
        List<String> parts = backpackList.stream().map(t->t.getParts()).collect(Collectors.toList());
        String partIdStr = StringUtils.join(parts,",");
        List<Long> partsLong= Arrays.stream(partIdStr.split(",")).map(s->Long.parseLong(s.trim())).distinct().collect(Collectors.toList());
        SysPart sysPart = new SysPart();
        sysPart.setPartIds(partsLong);
        //获取配件列表
        List<SysPart> partList = sysPartMapper.selectSysPartAll(sysPart);
        //构建配件名称
        for (SysBackpack backpack : backpackList) {
            List<SysPart> partOne = new ArrayList<>();
            StringBuffer partName = new StringBuffer();
            String[] partIds = backpack.getParts().split(",");
            for (String partId : partIds) {
                for (SysPart part : partList) {
                    if(part.getPartId().equals(Long.parseLong(partId))){
                        partName.append(",");
                        partName.append(part.getPartName());
                        //组装背包配件信息
                        SysPart sysPartTwo = new SysPart();
                        BeanUtils.copyProperties(part,sysPartTwo);
                        partOne.add(sysPartTwo);
                    }
                }
            }
            backpack.setPart(partOne);
            if (StringUtils.isNotBlank(partName.toString())) {
                backpack.setPartName(partName.toString().substring(1));
            }
        }

        return backpackList;
    }

    /**
     * 新增背包
     * 
     * @param sysBackpack 背包
     * @return 结果
     */
    @Override
    public int insertSysBackpack(SysBackpack sysBackpack)
    {
        sysBackpack.setParts(StringUtils.join(sysBackpack.getPartIds(),","));
        sysBackpack.setPartGroup(StringUtils.join(sysBackpack.getPartGroupIds(),","));
        sysBackpack.setCreateTime(DateUtils.getNowDate());
        return sysBackpackMapper.insertSysBackpack(sysBackpack);
    }

    /**
     * 修改背包
     * 
     * @param sysBackpack 背包
     * @return 结果
     */
    @Override
    public int updateSysBackpack(SysBackpack sysBackpack)
    {
        sysBackpack.setParts(StringUtils.join(sysBackpack.getPartIds(),","));
        sysBackpack.setPartGroup(StringUtils.join(sysBackpack.getPartGroupIds(),","));
        sysBackpack.setUpdateTime(DateUtils.getNowDate());
        return sysBackpackMapper.updateSysBackpack(sysBackpack);
    }

    /**
     * 批量删除背包
     * 
     * @param backpackIds 需要删除的背包主键
     * @return 结果
     */
    @Override
    public int deleteSysBackpackByBackpackIds(Long[] backpackIds)
    {
        return sysBackpackMapper.deleteSysBackpackByBackpackIds(backpackIds);
    }

    /**
     * 删除背包信息
     * 
     * @param backpackId 背包主键
     * @return 结果
     */
    @Override
    public int deleteSysBackpackByBackpackId(Long backpackId)
    {
        return sysBackpackMapper.deleteSysBackpackByBackpackId(backpackId);
    }
}
